#creat a peewee model with name AStock
import os

import peewee


current_dir = os.path.dirname(os.path.realpath(__file__))
# print(current_dir)
db = peewee.SqliteDatabase(current_dir + '/mydatabase.db')
# class BaseModel(peewee.Model):
#     class Meta:
#         database = db
class StocksModel(peewee.Model):
    # set peewee model table name
    class Meta:
        database = db
        table_name='astockmodel'
    id = peewee.AutoField()
    name = peewee.CharField(max_length=255)
    code = peewee.CharField(max_length=255)


class StocksDal():
    #find all AStockModel in database
    def findAll(self):
        return StocksModel.select()
    # get last code of AStockModel
    def getLastCode(self):
        return StocksModel.select().group_by(StocksModel.code).order_by(StocksModel.id.desc()).get().code

    # get all codes of AStockModel
    def getAllCode(self):
        return StocksModel.select(StocksModel.code).group_by(StocksModel.code).order_by(StocksModel.code.desc())

    #find AStockModel by date
    def findByDate(self,date):
        return StocksModel.select().where(StocksModel.code == date)

    def del_all_byDate(self,date):
        return StocksModel.delete().where(StocksModel.code == date).execute()
